package br.com.pharmproject.dao;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



import br.com.pharmproject.conexao.Conexao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import br.com.pharmproject.bean.Usuario;

/**
 *
 * @author rodrigo
 */
public class UsuarioDAO {
    
    Statement stm = null;
    
    public UsuarioDAO()throws ClassNotFoundException, SQLException{
        Conexao.getInstance();
        stm = Conexao.getInstance().getConector().createStatement();
    }
    
    public void consulta () throws SQLException{
        String sql = "SELECT idUsuario, user FROM usuario";
        ResultSet rs = stm.executeQuery(sql);
        
        
        while(rs.next()){
            System.out.println("User: " + rs.getString("user") 
                               +"\nPass: "+ rs.getString("pass"));
            Usuario usuario = new Usuario();
            usuario.setIdUsuario(rs.getInt("idUsuario"));
            usuario.setLogin(rs.getString("user"));
        
                    
        }
    }
    
    public boolean checkLogin(String user, String pass) throws SQLException {
        
        String sql = "SELECT * FROM USUARIO";
        
        ResultSet rs = null;
        try {
             rs = stm.executeQuery(sql);
            
            while(rs.next()){
            
                if(user.equals(rs.getString("user")) 
                        && pass.equals(rs.getString("pass"))){
                    return true;
                }
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            rs.close();
        }
 
        return false;
               
    }
    
    public void inserir(String user, String pass) throws SQLException{

      String sqlUpdate = "INSERT INTO usuario "
              + "(user, pass)" + " VALUES "
              + "('" + user + "', '" + pass +"')";

      stm.executeUpdate(sqlUpdate);
                
        
    }
}
